#include "tcp_client.h"
#include "ui_tcp_client.h"

tcp_client::tcp_client(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::tcp_client)
{
    ui->setupUi(this);

    tcpSever = new QTcpServer(this);
    tcpSocket = new QTcpSocket(this);

}

tcp_client::~tcp_client()
{
    delete ui;
}
void tcp_client::newConnection_SLOT()
{
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(socketReadReadySLOT()));
}
void tcp_client::socketReadReadySLOT()
{
    QString read_buf = tcpSocket->readAll();
    ui->plainTextEdit->appendPlainText(read_buf);
}
void tcp_client::on_pushButton_open_client_clicked()
{
    tcpSocket->connectToHost(ui->lineEdit_ip_address->text(), ui->lineEdit_port_num->text().toUInt());
    connect(tcpSocket, SIGNAL(connected()), this, SLOT(newConnection_SLOT()));
}

void tcp_client::on_pushButton_close_client_clicked()
{
    tcpSocket->close();
}

void tcp_client::on_pushButton_send_data_clicked()
{
    tcpSocket->write(ui->lineEdit_send_data->text().toLocal8Bit());
}
